/*
 * Rhy Youtube
 * 
 * @tag youtube.extra.rhy
 * @author Tristan Maindron
 * @dependencies rhy.core.ajax
 * 
 */

Rhy.extradata.youtubeServiceCallbacks = Array();

Rhy.extra.youtube = function(){
    this.serviceUrl = "http://gdata.youtube.com/feeds/api/";
    
    this.standardfeedsUrl = "standardfeeds";
    
    this.defaultOptions = {
        "v"             : "2",
        "alt"           : "jsonp"
    };
    
    this.configManager = Rhy.config.manager;
}

Rhy.extra.youtube.prototype.query = function(feed, options, callback){
    var o = this.configManager.merge(this.defaultOptions, options);

    o.callback = "Rhy.extradata.youtubeServiceCallbacks["+Rhy.extradata.youtubeServiceCallbacks.length+"]";
    o.alt = 'json';

    Rhy.extradata.youtubeServiceCallbacks[Rhy.extradata.youtubeServiceCallbacks.length] = callback;
    
    o = this.configManager.encode(o);

    var fullUrl = this.serviceUrl + feed + '?' + o;

    if(typeof(callback) != 'undefined')
        this.callback = callback;

    Rhy.ajax({
      url: fullUrl,
      type: 'jsonp'
    });
};

Rhy.extra.youtube.prototype.standard = function(type, options, callback){
    this.query(this.standardfeedsUrl + '/' + type, options, callback);
};
    
Rhy.extra.youtube.prototype.search = function(search, options, callback){
    options.q = search;
    this.query('videos', options, callback);
};